<?php
/**
 * 广告控制器
 * app端接口文件
 */
class AdvertAction extends Action{
	private $num = 4;
	
	//控制开关第三方广告平台
	//1 开 0 关
	public function AdvertButton(){
		$value =["code" => 1];
		header("Content-Type: text/json");
		die(et($value));
	}	
	//返回第三方平台的禁用列表
	public function getCancelAdvert(){
		$uid = t(dt($_REQUEST["uid"]));
		$mobile_unique_code = t($_SERVER["HTTP_USER_AGENT"]);
		$date = date("Ymd");
		if(!isset($_REQUEST["uid"])){
			$value = ["code"=>"empty","massage"=>"the Parameters is empty!!"];
			header("Content-Type: text/json");
			die(et($value));
		}else{
			$data = M("Advert_click_data")->where("click_time = '$date' and is_locate = 0 and (uid = $uid or mobile_unique_code = '$mobile_unique_code')")->field("aid")->select();
			if(mysql_num_rows($data) < 1){
				if(count($data)<1){
					$data=[];
				}
				$value = ["code" =>"success","data"=>$data];
				header("Content-Type: text/json");
				die(et($value));
			}else{
				$value = ["code" =>"wrong"];
				header("Content-Type: text/json");
				die(et($value));
			}
		}
		
	}
	//轮播图展示
	public function showBanner(){
		$date = date("Ymd");
		$sql ="select * from wz_advert_banner as b left join wz_advert as a on b.aid = a.aid where play_time = '$date' ";
		$data = M("Advert_banner")->query($sql);
		//判断广告数量是否足够			
		if(count($data)< $this->num){
			//不足够，则继续搜索自己的广告添加
			$number = $this->num - count($data);
			$data_back = M("Advert")->where("pid = 0")->order("rand()")->limit($number)->select();
			$acount = count($data);
			for($i=0;$i<count($data_back);$i++){
				$data[$acount+$i] = $data_back[$i];
			}
		}
		if (is_array($data)){
			for($i=0;$i<count($data);$i++){
				
				$image = D('attach')->where('attach_id='.$data[$i]['pic'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$data[$i]['pic'] = getImageUrl($image_url);
			}
			
			
			$value = ["code" =>"success","data"=>$data];
		}else{
			$value = ["code" =>"wrong","massage"=>M("Banner")->getDbError()];
		}
		
		header("Content-Type: text/json");
		die(et($value));
	}
	//列表数据展示:随机显示
	public function showAdert(){
		$tid = t(dt($_REQUEST["tid"]));
		if ($tid>0){
			$data = M("Adver")->where("is_display = 'y' and is_del = 'n' and is_forbidden = 'n' and tid = $tid")->select();
		}else{
			//$sql = "select title,pic,aid from ts_advert where is_display  = 'y' order by rand()";
			$data = M("Advert")->where("is_display  = 'y' and is_del = 'n' and is_forbidden = 'n'")->order("rand()")->select();
		}
		if ($data){
			$value = ["code" =>"success","data"=>$data];
		}else{
			if(mysql_num_rows($data) < 1){
				$value = ["code" =>"success","data"=>$data];
			}else{
				$value = ["code" =>"wrong"];
			}
		}
		header("Content-Type: text/json");
		die(et($value));
	}
		
	//分类列展示
	public function showAdvertType(){
		$type = M("Advert_type")->where("is_display = 'y'")->select();
		if ($type){
			$value = ["code" =>"success","data"=>$type];
			header("Content-Type: text/json");
			die(et($value));
		}else{
			if(mysql_num_rows($type) < 1){
				$value = ["code" =>"success","data"=>$type];
				header("Content-Type: text/json");
				die(et($value));
			}
			$value = ["code" =>"wrong"];
			header("Content-Type: text/json");
			die(et($value));
		}
	}
	//显示广告详细页
	public function showAdvertDetail(){
		$aid = t($_REQUEST["aid"]);
		$data = M("Advert")->where("aid = $aid")->find();
		
		if ($data){
			if($data["is_forbidden"] == "y"){
				$value = ["code" =>"wrong","massage"=>"该广告已经被禁，将无法查询"];
				header("Content-Type: text/json");
				die(et($value));
			}
			if($data["is_del"] == "y"){
				$value = ["code" =>"wrong","massage"=>"该广告已经删除，将无法查询"];
				header("Content-Type: text/json");
				die(et($value));
			}
			$value = ["code" =>"success","data"=>$data];
		}else{
			if(mysql_num_rows($data) < 1){
				$value = ["code" =>"success","data"=>$data];				
			}
			$value = ["code" =>"wrong"];			
		}
		header("Content-Type: text/json");
		die(et($value));
	}
		/*点击获取积分
		 * uid 用户ID
		 * aid	广告ID
		 * 
		 * */
	public function clickAdvert(){
		//获取参数
		$uid = t(dt($_REQUEST["uid"]));
		$aid = t(dt($_REQUEST["aid"]));
		$mobile_unique_code = t($_SERVER["HTTP_USER_AGENT"]);
		$is_locate = t(dt($_REQUEST["is_locate"]));
		$date = date("Ymd");
		$score = C("__SCORE__");
		if(!isset($_REQUEST["uid"]) && !isset($_REQUEST["aid"])){
			$value = ["code" =>"wrong","massage"=>"parameter is no complete!"];
			header("Content-Type: text/json");
			die(et($value));
		}
		//判断是否当天第一次
		$sql = "select count(*) from wz_advert_click_data where (uid = $uid or mobile_unique_code = $mobile_unique_code) and aid = $aid and click_time = $date and is_locate = $is_locate";
		$is_click = M("Advert_click_data")->query($sql)[0]["count(*)"];
		
		if ($is_click){
			//有数据，不是当天第一次
			$value = ["code" =>"wrong","massage"=>"no the first times today"];			
		}else{
			//无数据
			$advert = M("Advert");
			$advert->startTrans();
				//1.添加记录
				$click = M("Advert_click_data");
				$click->uid = $uid;
				$click->aid = $aid;
				$click->click_time = $date;
				$click->is_locate  =$is_locate;
			if($click->add()){
				//2.添加广告的点击数
				if($advert->where('aid= '.$aid)->setInc('visit_num')){
					//3.增加用户的积分
					if (M("Credit_user")->where("uid = $uid")->setInc("score",$score)){
						//全部操作成功
						$advert->commit();
						$this->masterScore($uid, $score, "click");
						$this->minPartnerClick($aid);
						$value=["code"=>"success"];
					}					
				} else{
					$advert->rollback();
					$value = ["code" =>"wrong","massage"=>"add click num wrong"];					
				}				
			} else{
				$advert->rollback();
				$value = ["code" =>"wrong","massage"=>"add click data wrong"];	
			}
		}
		header("Content-Type: text/json");
		die(et($value));
	}
	
	/**
	 * 分享获取积分
	 * 
	 */
	public function shareAdvert(){
		//获取参数
		$uid = t($_REQUEST["uid"]);
		$aid = t($_REQUEST["tid"]);
		$platform = t($_REQUEST["platform"]);
		$date = date("Ymd");
		$score = C("__SCORE__");
		if(!(isset($_REQUEST["uid"])&&isset($_REQUEST["aid"])&&isset($_REQUEST["platform"]))){
			$value = ["code" =>"wrong","massage"=>"parameter is no complete!"];
			header("Content-Type: text/json");
			die(et($value));
		}
		//判断是否当天第一次
		$sql = "select count(*) from ts_advert_share_data where uid = $uid and aid = $aid and click_time = $date and platform = '$platform'";
		$is_share = M("Advert_click_data")->query($sql)[0]["count(*)"];
		if ($is_share){
			//有数据，不是当天第一次
			$value = ["code" =>"wrong","massage"=>"no the first times today"];
		}else{
			//无数据
			$advert = M("advert");
			$advert->startTrans();
			//1.添加记录
			$share = M("Advert_share_data");
			$share->uid = $uid;
			$share->aid = $aid;
			$share->platform = $platform;
			$share->click_time = $date;
			if($share->add()){
				//2.添加广告的点击数
				if($advert->where('aid='.$aid)->setInc('share_num')){
					//3.增加用户的积分
					if (M("Credit_user")->where("uid = $uid")->setInc("score",$score)){
						//全部操作成功
						$advert->commit();
						$this->masterScore($uid, $score, "share");
						
						$value=["code"=>"success"];
					}
				} else{
					$advert->rollback();
					$value = ["code" =>"wrong","massage"=>"add share num wrong"];
				}
			} else{
				$advert->rollback();
				$value = ["code" =>"wrong","massage"=>"add share data wrong"];
			}
		}
		header("Content-Type: text/json");
		die(et($value));
	}
	
	//师徒关系分成
	public function masterScore($uid,$score,$type){
		//二级关联
		$f_score = $score/10;
		$s_score = $score/5;
			//第一级
		$f_master = M("User")->where("uid = $uid")->find()["master_id"];
		if($f_master>1){	
			//有师傅
				//添加师傅的积分
			if(M("Credit_user")->where("uid = $f_master")->setInc("score",$f_score)){
				//增加积分记录
				$record=M("Credit_master_record");
				$record->master_uid = $f_master;
				$record->tudi_uid = $uid;
				$record->level = 1;
				$record->date = date("Ymd");
				$record->type = $type;
				$record->score = $f_score;
				$record->add();	
			}
			//第二级
			//找师傅的师傅
			$s_master = M("User")->where("uid = $f_master")->find()["master_id"];
			if($s_master>1){
				//有师傅
					//添加师傅的积分
				if(M("Credit_user")->where("uid = $s_master")->setInc("score",$s_score)){
					//增加积分记录
					$record->master_uid = $f_master;
					$record->tudi_uid = $uid;
					$record->level = 2;
					$record->date = date("Ymd");
					$record->type = $type;
					$record->score = $s_score;
					$record->add();		
				}
			}	
		}
	}
	//获取积分详情	
	public function showUserCreditDedail(){
		$uid = t(dt($_REQUEST["uid"]));
		$where_c="";
		$where_m="";
		if (isset($_REQUEST["score_date_b"])){
			$score_date_b = t(dt($_REQUEST["score_date_b"]));
			$score_date_n = t(dt($_REQUEST["score_date_n"]));
			$where_c = " and click_time >= $score_date_b and click_time <= $score_date_n";
			$where_m = " and date >= $score_date_b and date <= $score_date_n";
		}
		
		//点击过的日子
		$sql = "select click_time,count(*)as count from wz_advert_click_data where uid= $uid ".$where_c." group by click_time desc  ";
		$click_data = M("Advert_click_data")->query($sql);
		
		//师徒分享
		$sql2 = "select date ,SUM(score) as count from wz_credit_master_record where master_uid= $uid ".$where_m." group by date desc";
		$master_data = M("Credit_master_record")->query($sql2);
		
		$data = array();
		for ($i=0;$i<count($click_data);$i++){
			$data[$click_data[$i]["click_time"]]['click']=$click_data[$i]["count"];
		}
		for($k=0;$k<count($master_data);$k++){
			if(in_array($master_data[$k]["date"],$data)){
				continue;
			}else{
				$data[$master_data[$k]["date"]]['master'] = $master_data[$k]["count"];
			}
		}
		
		
		if (count($data)<1){
			$value=["code"=>"success","data"=>[]];
		}else{
			$res=array();
			$num=0;
			foreach ($data as $k =>$v){
				$res[$num]["date"]=$k;
				$res[$num]["click"]=$v['click'];
				$res[$num]["master"]=$v['master'];
				$num++;
			}
			//dump($res);
			$value=["code"=>"success","data"=>$res];
		}
		
		header("Content-Type: text/json");
		die(et($value));
		
	}
	
	public function minPartnerClick($aid){
		$pid = M("Advert")->where("aid=$aid")->find()["pid"];
		M("Partner")->where("pid=$pid")->setDec("click_num");
	}
}